$(function loadShhet() {
    // var local_flag = true;
    var optdata = document.getElementById('option_data').value;

    //配置项
    var options = {
        container: 'luckysheet', //luckysheet为容器id
        // title: 'Luckysheet Demo', // 设定表格名称
        title: titlename,
        lang: 'zh', // 设定表格语言
        showinfobar: true,
        myFolderUrl: return_url, //作用：左上角<返回按钮的链接
        // allowUpdate: false,
        // updateImageUrl: "http://127.0.0.1:8001/markdown/uploadFileDiv/",
        // updateUrl: "ws://127.0.0.1:8001/ws/sheet/" + titlename + "/",
        // loadUrl: "http://127.0.0.1:8001/markdown/sheet/",
        // gridKey loadUrl的提交参数
        // gridKey: titlename,
        functionButton:
            '<button id="saveSheet" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">保存</button> '+
            '<button id="exitSheet" class="btn btn-primary btn-danger" style=" padding:3px 6px; font-size: 12px; margin-right: 85px;" >退出编辑</button>' +
            '<a href="/markdown/sheet/viewDemo/">LuckysheetDemo</a>' +
            '<a href="/markdown/sheetEdit/">Sheet</a>',
    };
    console.log(local_flag);
    console.log(titlename);
    console.log(return_url);
    // console.log('doc_optdata:' + optdata);

    if(local_flag == "True"){
        options.data = JSON.parse(optdata);

    }else {
        options.data = [
            {
                "name": "sheet1", //工作表名称
                "color": "", //工作表颜色
                "index": 0, //工作表索引
                "status": 1, //激活状态
                "order": 0, //工作表的下标
                "hide": 0,//是否隐藏
                "row": 25, //行数
                "column": 23, //列数
            },
            {
                "name": "Sheet2",
                "color": "",
                "index": 1,
                "status": 0,
                "order": 1,
                "row": 25, //行数
                "column": 23, //列数
                "celldata": [],
                "config": {}
            },
            {
                "name": "Sheet3",
                "color": "",
                "index": 2,
                "status": 0,
                "order": 2,
                "row": 25, //行数
                "column": 23, //列数
                "celldata": [],
                "config": {},
            }
        ]
    }
    luckysheet.create(options);
})

$(function () {
    $('#saveSheet').click(function (){
        var filename = document.getElementById('sheet_filename').value;
        var space_sheet = document.getElementById('sheet_file_space').value;
        console.log('fn' + filename);
        console.log('myFolderUrl' + space_sheet);
        var sheetcontent = luckysheet.getAllSheets();
        console.log(sheetcontent);
        $.post(
            '/markdown/queryEditor/',
            {'filename': filename, 'space': space_sheet},
            function (data) {
                console.log(data.flag);
                if(data.flag) {
                    console.log(data.tip);
                }else {
                    alert(data.tip);
                }
                console.log(data.filename);
                $.post(
                    '/markdown/saveSheet/',
                    {'content': JSON.stringify(sheetcontent), 'filename': data.filename, 'space': space_sheet},
                    function (data) {
                        console.log(data.res);
                        alert(data.res);
                    }
                )
            }
        );

    })
    }
)

$(function exitSheet(){
    $('#exitSheet').click(function () {
        var filename = document.getElementById('sheet_filename').value;
        $.get(
            '/markdown/exitEditor/?type=sheet&filename='+filename,
            function (data) {
                console.log(data);
                alert(data.tip);
            }
        )
    });
})